package org.csu.pet_store_2.Common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)//序列化的时候会把空值忽略
public class  CommonResponse<T> {
    private final int status;
    private  String msg;
    private  T data;//泛型


    private  CommonResponse(int status,String msg){
        this.status=status;
        this.msg=msg;
    }//只传数据
    private CommonResponse(int status,T data){
        this.data=data;
        this.status=status;
    }
    private CommonResponse(int status,String msg,T data){
        this.status=status;
        this.msg=msg;
        this.data=data;
    }
    @JsonIgnore
    public boolean isSuccess(){
        return this.status==ResponseCode.SUCCESS.getCode();
    }

    public static <T>CommonResponse<T> creatForSuccess(){
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getDesc());
    }
    public static <T>CommonResponse<T> creatForSuccessMessage(String msg){
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), msg);
    }
    public static <T>CommonResponse<T> creatForSuccess(String msg,T data){
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), msg,data);
    }
    public static <T>CommonResponse<T> creatForSuccess(T data){
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getDesc(),data);
    }
    public static <T>CommonResponse<T> creatForError(String  msg){
        return new CommonResponse<>(ResponseCode.ERROR.getCode(), msg);
    }
    public static <T>CommonResponse<T> creatForError(int code,String  msg){
        return new CommonResponse<>(code, msg);
    }

}
